import cloneDeep from 'lodash/cloneDeep';

//获取/日期
export const  getNow = () => {
    let _obj = {};
    let date = new Date();
    let y = date.getFullYear();
    let m = date.getMonth() + 1;
    let s = date.getDate();

    //不添加 0 
    let _m = m;
    let _s = s;

    _m = m;
    _s = s;
    m = m < 10 ? '0' + m : m;
    s = (s >= 0 && s <= 9) ? "0" + s : s;

   

    _obj = {
        year:y,
        month:m,
        strDate:s,
        _month:_m,
        _strDate:_s,
    }
    return _obj
} 

//获取 路由末尾 参数（非路由传递参数 eg: /home/:id）
export const  getRouteParam = (path) => {
    let key = '';
    let pathArr = path.split('/');
    key = pathArr[pathArr.length - 1];
    return key
} 

//截断最后一个逗号
export const cutOff = (ids) => {
    ids = ids.substring(0,ids.lastIndexOf(","));
    return ids;
}

//一维数组去重
export const doDeduplication = (arry) => {
    let temp = [];
    for(let index = 0; index < arry.length ;index++){
        if( !temp.includes(arry[index])){
            temp.push(arry[index]);
        }
    }

    return temp;
}

//比较两个日期的大小  YYYY-MM-DD
export const doCompareDate = (startDate,endDate) => {
    let startDateTemp = new Date(startDate);
    let endDateTemp = new Date(endDate);

    return startDateTemp > endDateTemp;
}

//比较两个日期是否相等  YYYY-MM-DD
export const isEqualDate = (startDate,endDate) => {
    let startDateTemp = new Date(startDate);
    let endDateTemp = new Date(endDate);

    return startDateTemp === endDateTemp;
}

//是否为 "" 或者 undifined
export const isUndifinedOrNull = (value) => {
    if(value === undefined || value === ""){
        return true;
    }

    return false;
}

//将十六进制的颜色转换成rgb
export const hex2Rgb = (hex) => { //十六进制转为RGB
    let rgb = []; // 定义rgb数组
    if (/^\#[0-9A-F]{3}$/i.test(hex)) { //判断传入是否为#三位十六进制数
        let sixHex = '#';
        hex.replace(/[0-9A-F]/ig, function(kw) {
            sixHex += kw + kw; //把三位16进制数转化为六位
        });
        hex = sixHex; //保存回hex
    }
    if (/^#[0-9A-F]{6}$/i.test(hex)) { //判断传入是否为#六位十六进制数
        hex.replace(/[0-9A-F]{2}/ig, function(kw) {
        rgb.push(eval('0x' + kw)); //十六进制转化为十进制并存如数组
    });
    return `${rgb.join(',')}`; //输出RGB格式颜色

    } else {
        return '0,0,0';
    }
}

//获取当前设备
export const getEquipment = () =>{
    let u = window.navigator.userAgent;
    let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    let isEMobile = u.indexOf('E-Mobile') > -1;
    let isDT = u.indexOf('DingTalk') > -1;

    let reg = /MicroMessenger/i;
    let isWx = reg.test(u);

    if(!isWx){
        isWx = '';  //和微信无关
    }else{
        let isWxWork = u.match(/WxWork/i) == "wxwork";
        if(isWxWork){
            isWx = false; // 企业微信
        }else{
            isWx = true;    //微信
        }
    }

    let equipment = {
        isAndroid:isAndroid,
        isiOS:isiOS,
        isEMobile:isEMobile,
        isWx:isWx,
        isDT:isDT,

    }
    console.log('equipment',equipment);
    return equipment
}

export const getGuid = () =>{
    return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}

const S4 = () =>{
    return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}

//获取地址栏的参数
export const getUrlParam = (url) =>{
    if(url == '') return '';
    let temp1 = url.split('?');
    let pram = temp1[1];
    let keyValue = pram.split('&');
    let obj = {};
    for (let i = 0; i<keyValue.length; i++){
        let item = keyValue[i].split('=');
        let key = item[0];
        let value = item[1];
        obj[key] = value;
    }

    return obj;
}

//数组去重
export const getNoRepeatArr = (arr) =>{
    let obj = {};
    let newArr = [];
    let repeatNum = [];
    for(let i=0;i<arr.length;i++){
        if(newArr.indexOf(arr[i]) == -1){
            newArr.push(arr[i]);
        }else{
            repeatNum.push(i);
        }
    }
    
    obj['arr'] = newArr;
    obj['num'] = repeatNum;

    return obj;
}

//千分位
export const toThousands =(num)=> {
    let result = '',
    counter = 0;
    let isNumber = getNumber(num);
    if(isNumber){
        let intNum = num.split('.')[0];
        let floatNum = num.split('.')[1];
       
        intNum = (intNum || 0).toString();
        
        for (var i = intNum.length - 1; i >= 0; i--) {
            counter++;
            result = intNum.charAt(i) + result;
            
            if (!(counter % 3) && i != 0) {
                result = ',' + result;
            }
        }
        
        result = result + '.' + floatNum;
        
        return result;
    }
    return result;
}

//去掉千分位
export const takeOutThousands = (num) =>{
    let result = '';
    if(num && num != undefined && num != null){
        result = num.replace(/,/gi,'');
    }
    
    return result;
}

const getNumber = (num)=>{
    let reg_num = /^(\-|\+)?[0-9]+(\.[0-9]+)?$/; //不区分正负数，小数
    return reg_num.test(num);
}

export const dataFormatting = (fmt, date) => {
    let ret;
    let opt = {
      "Y+": date.getFullYear().toString(),        // 年
      "m+": (date.getMonth() + 1).toString(),     // 月
      "d+": date.getDate().toString(),            // 日
      "H+": date.getHours().toString(),           // 时
      "M+": date.getMinutes().toString(),         // 分
      "S+": date.getSeconds().toString()          // 秒
      // 有其他格式化字符需求可以继续添加，必须转化成字符串
    };
    for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
        };
    };
    return fmt;
}

/*
**  深层 map 改变 重新刷新数据
*/

export const regainMap = (preMap) =>{
    let tempMap = cloneDeep(preMap);
    let newMap = new Map();
    for(let [key,value] of tempMap){
        newMap.set(key,value)
    }
    return newMap
}

//去掉em基本信息前两位
export const filterColor = (emColor,number = 2) =>{
    let _color = emColor.slice(number);
    return _color;
    
}

//获取多语言
export const getLang = () =>{
    let lang = (window.ua && window.ua.language) || 'zh_CN';
    return lang;
}

//二级路径改造
export const getEcPath = () =>{
    return (window.ecologyContentPath || "");
}